#!/bin/bash
##################################
source ~/scriptbelt/bashutilities.sh
##################################

times=9
dolist=false
domerge=false
for iarg in $*; do
    CLIinputs[$argCounter]=$iarg


    option=`echo "$iarg" | cut -d"=" -f1`

    if [ "$option" == "--list" ]; then
        dolist=true
        echo -e "will list runs "
    fi
    if [ "$option" == "--merge" ]; then
        domerge=true
        echo -e "will merge runs "
    fi
    if [ "$option" == "--times" ]; then
        times=`echo "$iarg" | cut -d"=" -f2`
        echo -e "will do it " $times " times"
    fi
    if [ "$option" == "--id" ]; then
        idrun=`echo "$iarg" | cut -d"=" -f2`
        echo -e "will start with id="$idrun 
	fi
    if [ "$option" == "-h" ]; then

        echo "available options:"
        echo "--merge "
        echo "--list"
        echo "--times[=9]"
        echo "--id=125816 [no default]"
         exit
    fi
done

if [ $dolist = false ]; then
	if [ $domerge = false ]; then	
		echo "neither list nor merge has been asked for, nothing to do."
		exit
	fi
fi

if [ -n "$idrun" ]; then
	if [ $dolist = true ]; then
		for ((i=0; i < $times; ++i)); do 
			counter=$(($idrun + $i ))
			echo "iterator=$i: id=$counter has "`ls *$counter*.gnu | wc -l`" files";
		done
	fi



	if [ $domerge = true ]; then
		for ((i=0; i < $times; ++i)); do 
			counter=$(($idrun + $i ))
			echo "iterator=$i: id=$counter has "`ls *$counter*.gnu | wc -l`" files";
			echo "merge_gnuplot.py "$counter" > "$counter".log"
			merge_gnuplot.py $counter > $counter".log"
		done
	fi
fi

if [ -z "$idrun" ]; then
	echo "need to specify the ID with --id=12334567"
fi